package com.dmzc.priorityqueue;

import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * 12.MaxHeap的解法
 */
public class PriorityQueue2 {


    public static void main(String[] args) {
        Solution so = new Solution();
        int nums[] = {1, 3, 1, 2, 0, 5};
        int[] arr = so.maxSlidingWindow(nums, 3);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

}

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        PriorityQueue queue = new PriorityQueue();
        int arrNew[] = new int[nums.length - k + 1];
        for (int i = 0; i < nums.length - k + 1; i++) {
            if (i > 0) {
                queue.remove(new Fqueue(nums[i - 1], i - 1));
                queue.add(new Fqueue(nums[i + k - 1], i + k - 1));
            } else {
                for (int j = 0; j < k; j++) {

                    queue.add(new Fqueue(nums[i + j], i + j));
                }
            }


            arrNew[i] = ((Fqueue) queue.peek()).getValue();
        }

        return arrNew;
    }
}

class Fqueue implements Comparable {
    private int value;
    private int index;

    public int getValue() {
        return value;
    }

    public int getIndex() {
        return index;
    }

    public Fqueue(int value, int index) {
        this.value = value;
        this.index = index;
    }

    @Override
    public int compareTo(Object o) {
        Fqueue fq = (Fqueue) o;
        return fq.value - this.value;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Fqueue fqueue = (Fqueue) o;
        return value == fqueue.value &&
                index == fqueue.index;
    }

    @Override
    public int hashCode() {
        return Objects.hash(value, index);
    }
}

